package com.xjl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.xjl.dao.GoodsDao;
import com.xjl.demo.Admin;
import com.xjl.demo.Goods;
import com.xjl.demo.User;
import com.xjl.util.DBUtil;

public class GoodsDaoImpl implements GoodsDao {

	//查询所有商品信息
	public List<Goods> QueryAllGoods(int pageStart, int pageSize) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Goods> list = new ArrayList<Goods>();
		try {
			conn = DBUtil.getConnection();
			String sql = "SELECT * FROM goods limit ?,?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, pageStart);
			stmt.setInt(2, pageSize);
			rs = stmt.executeQuery();
			while(rs.next()){
				Goods gd =new Goods();
				gd.setGid(rs.getInt("gid"));
				gd.setCid(rs.getString("cid"));
				gd.setGname(rs.getString("gname"));
				gd.setBrand(rs.getString("brand"));
				gd.setGimage(rs.getString("gimage"));
				gd.setContent(rs.getString("content"));
				gd.setShelftime(rs.getString("shelftime"));
				gd.setGprice(rs.getDouble("gprice"));
				gd.setGstock(rs.getInt("gstock"));
				list.add(gd);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs, stmt, conn);
		}
		
		return list;
	}

	//查询所有商品数量
	public int CountGoodsDao() {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs=null;
		int index = 0;
		try {
			conn = DBUtil.getConnection();
			String sql = "SELECT count(*) FROM goods";
			stmt = conn.prepareStatement(sql);
			rs=stmt.executeQuery();
			if(rs.next()){
				index=rs.getInt(1);
			}
		}catch (Exception e) {
			index=0;
			//e.printStackTrace();	
		}finally{
			DBUtil.close(rs, stmt, conn);
		}
		return index;
	}

}


